// TreeCtrlSubclass.cpp: implementation of the CTreeCtrlSubclass class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "skin.h"
#include "TreeCtrlSubclass.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

COLORREF CTreeCtrlSubclass::m_crFrmNor = RGB(100, 100, 100);
COLORREF CTreeCtrlSubclass::m_crFrmFoc = RGB(0, 0, 255);

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTreeCtrlSubclass::CTreeCtrlSubclass()
{

}

CTreeCtrlSubclass::~CTreeCtrlSubclass()
{

}

LRESULT CTreeCtrlSubclass::WindowProc(UINT msg, WPARAM wp, LPARAM lp)
{
	switch (msg){
		case WM_PAINT:
			OnPaint();
			return 0;
		case WM_NCPAINT:
			CWndSubclass::WindowProc(msg, wp, lp);
			OnNcPaint();
			return 0;
		case WM_SETFOCUS:
			CWndSubclass::WindowProc(msg, wp, lp);
			OnNcPaint();
			return 0;
		case WM_KILLFOCUS:
			CWndSubclass::WindowProc(msg, wp, lp);
			OnNcPaint();
			return 0;
	}
	return CWndSubclass::WindowProc(msg, wp, lp);
}

void CTreeCtrlSubclass::OnNcPaint()
{	
	CWindowDC dcWin(m_pWndHooked);
	dcWin.SetBkMode(TRANSPARENT);

	CRect rcWin;
	m_pWndHooked->GetWindowRect(&rcWin);
	rcWin -= rcWin.TopLeft();

	if (::GetFocus() == m_pWndHooked->m_hWnd)
	{
		CRect rcLeftFrm(rcWin);
		rcLeftFrm.right = rcLeftFrm.left + 2;
		dcWin.Draw3dRect(&rcLeftFrm, RGB(160, 180, 200), RGB(100, 120, 140));

		CRect rcTopFrm(rcWin);
		rcTopFrm.bottom = rcTopFrm.top + 2;
		dcWin.Draw3dRect(&rcTopFrm, RGB(160, 180, 200), RGB(100, 120, 140));

		CRect rcRightFrm(rcWin);
		rcRightFrm.left = rcRightFrm.right - 2;
		dcWin.Draw3dRect(&rcRightFrm, RGB(100, 120, 140), RGB(160, 180, 200));

		CRect rcBottomFrm(rcWin);
		rcBottomFrm.top = rcBottomFrm.bottom - 2;
		dcWin.Draw3dRect(&rcBottomFrm, RGB(100, 120, 140), RGB(160, 180, 200));
	}
	else
	{
		/*CRect rcLeftFrm(rcWin);
		rcLeftFrm.right = rcLeftFrm.left + 2;
		dcWin.Draw3dRect(&rcLeftFrm, RGB(150, 150, 150), RGB(100, 100, 100));

		CRect rcTopFrm(rcWin);
		rcTopFrm.bottom = rcTopFrm.top + 2;
		dcWin.Draw3dRect(&rcTopFrm, RGB(150, 150, 150), RGB(100, 100, 100));

		CRect rcRightFrm(rcWin);
		rcRightFrm.left = rcRightFrm.right - 2;
		dcWin.Draw3dRect(&rcRightFrm, RGB(200, 200, 200), RGB(250, 250, 250));

		CRect rcBottomFrm(rcWin);
		rcBottomFrm.top = rcBottomFrm.bottom - 2;
		dcWin.Draw3dRect(&rcBottomFrm, RGB(200, 200, 200), RGB(250, 250, 250));*/
		CRect rcLeftFrm(rcWin);
		rcLeftFrm.right = rcLeftFrm.left + 2;
		dcWin.Draw3dRect(&rcLeftFrm, RGB(200, 200, 200), RGB(250, 250, 250));

		CRect rcTopFrm(rcWin);
		rcTopFrm.bottom = rcTopFrm.top + 2;
		dcWin.Draw3dRect(&rcTopFrm, RGB(200, 200, 200), RGB(250, 250, 250));

		CRect rcRightFrm(rcWin);
		rcRightFrm.left = rcRightFrm.right - 2;
		dcWin.Draw3dRect(&rcRightFrm, RGB(200, 200, 200), RGB(250, 250, 250));

		CRect rcBottomFrm(rcWin);
		rcBottomFrm.top = rcBottomFrm.bottom - 2;
		dcWin.Draw3dRect(&rcBottomFrm, RGB(200, 200, 200), RGB(250, 250, 250));
	}
}

void CTreeCtrlSubclass::OnPaint()
{
	CPaintDC dc(m_pWndHooked); 
	
	CRect rcClient;
	m_pWndHooked->GetClientRect(&rcClient);

	CDC memDC;
	memDC.CreateCompatibleDC(&dc);	
	CBitmap bitmap;
	bitmap.CreateCompatibleBitmap(&dc, rcClient.Width(), rcClient.Height());
	memDC.SelectObject(&bitmap);

	CWndSubclass::WindowProc(WM_PAINT, (WPARAM)memDC.m_hDC , 0);

	CDC maskDC;
	maskDC.CreateCompatibleDC(&dc);
	CBitmap maskBitmap;
	maskBitmap.CreateBitmap(rcClient.Width(), rcClient.Height(), 1, 1, NULL);
	maskDC.SelectObject(&maskBitmap);
	maskDC.BitBlt(0, 0, rcClient.Width(), rcClient.Height(), &memDC, 
		rcClient.left, rcClient.top, SRCCOPY);
	
	CBrush brush(RGB(255, 255, 200));
	//brush.CreatePatternBrush(&m_bitmap);
	//dc.FillRect(rcClient, &brush);	
	DoGradientFill(&dc, &rcClient);
	
	memDC.SetBkColor(RGB(0,0,0));          
	memDC.SetTextColor(RGB(255,255,255));  
	memDC.BitBlt(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), &maskDC, rcClient.left, rcClient.top, SRCAND);	
	
	dc.SetBkColor(RGB(255,255,255));
	dc.SetTextColor(RGB(0,0,0));
	dc.BitBlt(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), &maskDC, rcClient.left, rcClient.top, SRCAND);
	
	dc.BitBlt(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), &memDC, 
					rcClient.left, rcClient.top, SRCPAINT);
	brush.DeleteObject();
}

BOOL CTreeCtrlSubclass::SubclassWindow(CWnd* pRealWnd)
{
	CWndSubclass::SubclassWindow(pRealWnd);
	return 0;
}

BOOL CTreeCtrlSubclass::UnSubclassWindow()
{
	return CWndSubclass::UnSubclassWindow();
}

void CTreeCtrlSubclass::DoGradientFill(CDC *pDC, CRect* rect)
{
	CBrush brBk[64];
	int nWidth = rect->Width();	
	int nHeight = rect->Height();
	CRect rct;
	
	for (int i = 0; i < 64; i ++)
	{
		brBk[i].CreateSolidBrush(RGB(255 - (i / 3), 255 - (i / 3), 255 - (i / 5)));
	}
	
	for (i = rect->top; i <= nHeight + 2; i ++) 
	{
		rct.SetRect(rect->left, i, nWidth + 2, i + 1);
		pDC->FillRect(&rct, &brBk[((i * 63) / nHeight)]);
	}
	
	for (i = 0; i < 64; i ++)
		brBk[i].DeleteObject();
}